package com.gzhu.knowledgeAdmin.controller;

import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.valid.AddGroup;
import com.gzhu.knowledgeAdmin.common.valid.UpdateGroup;
import com.gzhu.knowledgeAdmin.common.vo.TypeTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.gzhu.knowledgeAdmin.entity.Zsfl;
import com.gzhu.knowledgeAdmin.service.ZsflService;

import java.util.Map;
import java.util.List;

/**
 * 知识分类表
 *
 * @author giaogiao
 * @email giaogiao@gmail.com
 * @date 2023-04-22 23:18:59
 */
@Api(tags = "知识分类控制器")
@RestController
@RequestMapping("knowledgeAdmin/zsfl")
@CrossOrigin
public class ZsflController {
    @Autowired
    private ZsflService zsflService;

    /*
     * 查出所有分类以及子分类，以树形结构返回
     * */
    @GetMapping("list/tree")
    @ApiOperation("查出所有分类以及子分类，以树形结构返回")
    @PreAuthorize("hasAnyAuthority('knowledgeType.list','knowledge.add')")
    public R list(@RequestParam Map<String, Object> params){
        List<TypeTreeVo> list = zsflService.getTree();
        return R.ok(list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('knowledgeType.update')")
    public R info(@PathVariable("id") Integer id){
		Zsfl zsfl = zsflService.getById(id);
        return R.ok(zsfl);
    }

    /**
     * 添加
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('knowledgeType.add')")
    public R save(@RequestBody @Validated(AddGroup.class) Zsfl zsfl){
		zsflService.saveZsfl(zsfl);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('knowledgeType.update')")
    public R update(@RequestBody @Validated(UpdateGroup.class) Zsfl zsfl){
            zsflService.updateById(zsfl);
        return R.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('knowledgeType.delete')")
    public R delete(@RequestBody List<Integer> ids){
		zsflService.removeZsflByIds(ids);
        return R.ok();
    }

}
